package kMesh;

public class IndexList {
	private int[] values;

	/*
	 * CONSTRUCTORS
	 * 
	 * 
	 * 
	 */

	public IndexList() {
		values = new int[0];
	}

	public IndexList(int[] initalData) {
		values = initalData;
	}

	/*
	 * GETTING
	 * 
	 * 
	 * 
	 */

	public int count() {
		return values.length;
	}

	public int getAt(int i) {
		return values[i];
	}

	public int[] toArray() {
		return values;
	}
	
	public String toString() {
		String str = "[";
		for (int n = 0; n < values.length; n++) {
			if (n==values.length-1){str+=values[n];}
			else{str+=values[n]+",";}
		}
		str += "] ";
		return str;
	}

	/*
	 * SETTING
	 * 
	 * 
	 * 
	 */

	public boolean setAt(int index, int value) {
		values[index] = value;
		return true;
	}
	
	public boolean append(int i) {
		int[] ret = new int[values.length + 1];
		for (int n = 0; n < values.length; n++) {
			ret[n] = values[n];
		}
		ret[values.length] = i;
		values = ret;
		return true;
	}

	public boolean append(int[] arr) {
		int[] ret = new int[values.length + arr.length];
		int x = 0;
		for (int n = 0; n < values.length; n++) {
			ret[x] = values[n];
			x++;
		}
		for (int n = 0; n < values.length; n++) {
			ret[x] = values[n];
			x++;
		}
		values = ret;
		return true;
	}

	public boolean append(IndexList list) {
		int[] arr = list.toArray();
		return append(arr);
	}

	/*
	 * MANIPULATION
	 * 
	 * 
	 * 
	 */

	public int popAt(int index){
		int ret = values[index];
		removeAt(index);
		return ret;
	}
	
	public boolean removeAt(int index){
		int[] ret = new int[values.length - 1];
		int x = 0;
		for (int i = 0; i < values.length; i++) {
			if (i != index) {
				ret[x] = values[i];
				x++;
			}
		}
		values = ret;
		return true;
	}
	
	public void reindexDown(int horizonValue) {
		for (int i = 0; i < values.length; i++) {
			if (values[i] > horizonValue) {
				values[i]--;
			}
		}
	}

	public void removeDups() {
		IndexList ret = new IndexList();
		for (int i = 0; i < values.length; i++) {
			if (!ret.containsValue(values[i])) {ret.append(values[i]);}
		}
		values = ret.values;
	}

	/*
	 * QUERY
	 * 
	 * 
	 * 
	 */

	/**
	 * looks for a value in the list of values. if found, returns its index,
	 * otherwise returns -1
	 * 
	 * @param valueToLookFor
	 */
	public int indexOfValue(int valueToLookFor) {
		for (int i = 0; i < values.length; i++) {
			if (values[i] == valueToLookFor) {
				return i;
			}
		}
		return -1;
	}
	
	public boolean removeIfFound(int valueToRemove) {
		int i = indexOfValue(valueToRemove);
		if (i>=0){
			removeAt(i);
			return true;
		} else {
			return false;
		}
	}

	public boolean containsValue(int valueToLookFor) {
		return indexOfValue(valueToLookFor) >= 0;
	}

}
